<?php


namespace SidToken\Controller;


use Think\Controller;

class BaseController extends Controller
{
    public function jsuccess($data = '', $msg = '')
    {
        $this->json(1, $msg, $data);
        exit;
    }

    public function jerror($msg = '', $ret = 10000, $data = '')
    {
        $this->json($ret, $msg, $data);
        exit;
    }

    public function json($ret = 0, $msg = '', $data = '')
    {
        header('Content-Type:application/json; charset=utf-8');
        header('Access-Control-Allow-Methods:*');
        header('Access-Control-Allow-Headers:*');
        header("Access-Control-Request-Headers:*");
        $return = array(
            'status' => $ret,
            'info' => $msg,
            'data' => $data
        );
        echo json_encode($return);
        exit;
    }

    /**
     * 执行时间 校验
     * @param $key
     * @param $interval
     */
    function timeToDoTask($key, $interval)
    {
        $lastExeTime = redis($key);
        if ($lastExeTime) {
            if (time() - $lastExeTime < $interval) {
                echo "执行时间未到,现在时间：" . date("Y-m-d H:i:s") . "上次执行时间：" . date("Y-m-d H:i:s", $lastExeTime) . "规定执行时间间隔：{$interval}s<br>";
                return false;
            } else {
                $lastExeTime = time();
                redis($key, $lastExeTime);

            }
        } else {
            $lastExeTime = time();
            redis($key, $lastExeTime);
        }
        return true;
    }
}